{extend name="common@admin_base" /}

{block name="body"}
<form action="{$_form.admin_group_save}" method="post" class="layui-form" lay-filter="user_form">
    <div class="layui-form-item">
        <label class="layui-form-label">组名<span style="color: red">*</span></label>
        <div class="layui-input-block">
            <input name="groupName" lay-verify="required" autocomplete="off" placeholder="请输入组名" class="layui-input" type="text">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选择权限</label>
        <div class="layui-input-block">
            <div id="auths"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <input class="layui-btn" id="submit" lay-submit="" lay-filter="user_form" type="submit" value="立即提交"/>
        </div>
    </div>
</form>
{/block}


{block name="head_extends"}
<script type="application/javascript">
    layui.config({
        base: '/static/js/',
    }).extend({
        authtree: '',
    });
    layui.use(['jquery', 'authtree', 'form'], function(){
        var $ = layui.jquery;
        var authtree = layui.authtree;
        var form = layui.form;
        $.ajax({
            url: '{$_form.admin_group_authlist}1',
            dataType: 'json',
            success: function(data){
                console.log(data);
                // 渲染时传入渲染目标ID，树形结构数据（具体结构看样例，checked表示默认选中），以及input表单的名字
                authtree.render('#auths', data.data, {inputname: 'authids[]', layfilter: 'lay-check-auth', openall: false});
                form.on('checkbox(lay-check-auth)', function(data){
                    // 注意这里：需要等待事件冒泡完成，不然获取叶子节点不准确。
                    setTimeout(function(){
                        // 获取选中的叶子节点
                        var leaf = authtree.getLeaf('#auths');
                        console.log(leaf);
                    }, 100);
                });
            }
        });
        form.on('submit(user_form)',function (data) {
            console.log(data.field);
            $.ajax({
                url:'{$_form.admin_group_save}'
                ,data:data.field
                ,method:'post'
                ,success:function (data) {
                    console.log(data);
                    if (typeof data == 'string'){
                        data = JSON.parse(data);
                    }
                    parent.layer.alert(data.info);
                    if (data.msg == 1){
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    }
                }
            });
            return false;
        });
    });
</script>
{/block}